#!/bin/bash
#FALTA PASAR EL INPUTLOG POR PARAMETRO
#INPUT: com.autodesk.akn/akamai_logs/beehive_autodesk_com_176461.esw3ccust_S.201212110000-2400-0.gz


#echo "Write the path to the input bucket."
#echo "Connecting to the cluster.."

input=$1

#input="com.autodesk.akn/akamai_logs/beehive_autodesk_com_176461.esw3ccust_S.201212060000-2400-1.gz"
#Job Parserrrrr
intermediate=${input#*'beehive_autodesk_com_176461.esw3ccust_S.'}
jobFolder=${intermediate%'.'*}

output=$(./elastic-mapreduce --create --name "Log Processing:"$jobFolder --bootstrap-action s3://com.autodesk.akn/testProcesses/BDFunctions/configureHadoopJob/configureHadoop.sh --log-uri s3n://com.autodesk.akn/ProcessedLogs/logs) 

jobID=${output##*flow }
echo "Job Id:"$jobID

echo "Folder com.autodesk.akn/ProcessedLogs/"$jobFolder

echo  "------------------------------------------------------------------"
echo "Process that finds the new Registers"

./elastic-mapreduce $jobID \
	  --jar s3n://com.autodesk.akn/testProcesses/BDFunctions/mapred-poc-0.0.1-SNAPSHOT.jar --step-name "Look for new Regs Logs" --step-action CONTINUE \
	  --main-class Autodesk/LookForNotFound \
	  --arg s3n://$input \
	  --arg s3n://com.autodesk.akn/ProcessedLogs/ExtraInformation/NewLogs 

echo  "------------------------------------------------------------------"
echo "Process that updates the database, inserts new registers and sends the emails for registers not found"

./elastic-mapreduce $jobID \
	  --jar s3n://com.autodesk.akn/testProcesses/BDFunctions/mapred-poc-0.0.1-SNAPSHOT.jar --step-name "Check WebService and Regs to Database" --step-action CONTINUE \
	  --main-class WebserviceChecker/CheckWebServiceProcess \
	  --arg s3n://com.autodesk.akn/ProcessedLogs/ExtraInformation/NewLogs/part-r-00000 \
	  --arg s3n://com.autodesk.akn/ProcessedLogs/ExtraInformation/RegsToEmail 

echo  "------------------------------------------------------------------"
echo "Process that processes the log files"

./elastic-mapreduce $jobID \
	  --jar s3n://com.autodesk.akn/testProcesses/BDFunctions/mapred-poc-0.0.1-SNAPSHOT.jar --step-name "Parsed Logs" --step-action CONTINUE \
	  --main-class Autodesk/ParseLogs \
	  --arg s3n://$input \
	  --arg s3n://com.autodesk.akn/ProcessedLogs/ExtraInformation/ParsedLogs \
	  --arg '-ip' \
	  --arg s3n://com.autodesk.akn/testProcesses/BDFunctions/MappedIpExcluded 

echo  "------------------------------------------------------------------"
echo "Process that builds the volume report"

./elastic-mapreduce $jobID \
  --jar s3n://com.autodesk.akn/testProcesses/BDFunctions/mapred-poc-0.0.1-SNAPSHOT.jar --step-name "Volume Filter" --step-action CONTINUE \
  --main-class Autodesk/TotalVolumeFilter \
  --arg s3n://com.autodesk.akn/ProcessedLogs/ExtraInformation/ParsedLogs \
  --arg s3n://com.autodesk.akn/ProcessedLogs/toDatabase/akn_traffic_fact

echo  "------------------------------------------------------------------"
echo "Process that builds the word report"

./elastic-mapreduce $jobID \
  --jar s3n://com.autodesk.akn/testProcesses/BDFunctions/mapred-poc-0.0.1-SNAPSHOT.jar --step-name "WordFilter" --step-action CONTINUE  \
  --main-class Autodesk/WordFilter \
  --arg s3n://com.autodesk.akn/ProcessedLogs/ExtraInformation/ParsedLogs \
  --arg s3n://com.autodesk.akn/ProcessedLogs/toDatabase/akn_word_fact \
  --arg '-arch' \
  --arg s3n://com.autodesk.akn/testProcesses/BDFunctions/excludedWords

echo  "------------------------------------------------------------------"
echo "Process that builds the query report"

./elastic-mapreduce $jobID \
  --jar s3n://com.autodesk.akn/testProcesses/BDFunctions/mapred-poc-0.0.1-SNAPSHOT.jar --step-name "QueryReport" --step-action CONTINUE \
  --main-class Autodesk/TotalQueryFilter \
  --arg s3n://com.autodesk.akn/ProcessedLogs/ExtraInformation/ParsedLogs \
  --arg s3n://com.autodesk.akn/ProcessedLogs/toDatabase/akn_query_fact

echo  "------------------------------------------------------------------"
echo "Process that builds the session report"

./elastic-mapreduce $jobID \
  --jar s3n://com.autodesk.akn/testProcesses/BDFunctions/mapred-poc-0.0.1-SNAPSHOT.jar --step-name "SessionFilter" --step-action CONTINUE  \
  --main-class Autodesk.SessionFilter \
  --arg s3n://com.autodesk.akn/ProcessedLogs/ExtraInformation/ParsedLogs \
  --arg s3n://com.autodesk.akn/ProcessedLogs/ExtraInformation/SessionFilterNotReady

./elastic-mapreduce flow $jobID \
  --jar s3n://com.autodesk.akn/testProcesses/BDFunctions/mapred-poc-0.0.1-SNAPSHOT.jar --step-name "SessionFilterCounter" \
  --main-class Autodesk/SessionFilterCounter \
  --arg s3n://com.autodesk.akn/ProcessedLogs/ExtraInformation/SessionFilterNotReady/part-r-00000 \
  --arg s3n://com.autodesk.akn/ProcessedLogs/toDatabase/akn_session_fact \
  --arg '-dayTime' \
  --arg s3n://com.autodesk.akn/testProcesses/BDFunctions/MappedDayMinute




